# 版本信息： V1.0.1
# 功能：  拖动页面设计案例

#!/usr/bin/python
# -*- coding: UTF-8 -*-
from tkinter import *
import tkinter.font as tf
from tkinter import ttk
import json
import threading
class TkinterAutoDrag:
    class AutoDragWidgetInfo:        
        def __init__(self,Name,Wdget):
            self.WidgetName = Name
            self.Widget = Wdget
     
    """
    控件拖动实现
    """
    #框架控件存放列表
    FrameWidgetList = []
    #控件存放列表
    WidgetList = []
    #选中的控件
    WidgetListSelect = []
    #拖拽时 左键按下时的偏移量
    offset_x = 0  
    offset_y = 0
    #拖拽时 右键按下时的偏移量
    offset3_x = 0  
    offset3_y = 0
    
    # 根据控件名称获取控件
    def FindFrameWidget(self,Name): 
        Offset = 0
        for WidgetInfo in self.FrameWidgetList:            
            if(Name == WidgetInfo.WidgetName):
                return 0,WidgetInfo.Widget
        print("not find widget name=",Name)
        return -1,"NULL"
    #根据控件找到框架控件的绑定信息
    def FindFrameWidgetInfoByWidget(self,Widget): 
        Offset = 0
        for WidgetInfo in self.FrameWidgetList:            
            if(Widget == WidgetInfo.Widget):
                return 0,WidgetInfo

        print("not find widget name=",Widget['text'])
        return -1,"NULL"
    def DelFrameWidgetInfoByWidget(self,Widget): 
        Offset = 0
        for WidgetInfo in self.FrameWidgetList:            
            if(Widget == WidgetInfo.Widget):
                self.FrameWidgetList.pop(Offset)
                self.FrameWidgetNameUpdate()
                return 1
            Offset += 1

        return 0
    
    #根据控件找到控件的绑定信息
    def FindWidgetInfoByWidget(self,Widget): 
        Offset = 0
        for WidgetInfo in self.WidgetList:            
            if(Widget == WidgetInfo.Widget):
                return 0,WidgetInfo

        print("FindWidgetInfoByWidget not find widget name=",Widget['text'])
        return -1,"NULL"
    
    def DelWidgetInfoByWidget(self,Widget): 
        Offset = 0
        for WidgetInfo in self.WidgetList:            
            if(Widget == WidgetInfo.Widget):
                self.WidgetList.pop(Offset)
                return 1
            Offset += 1

        return 0
    def DelSelectWidgetInfoByWidget(self,Widget): 
        Offset = 0
        for WidgetInfo in self.WidgetListSelect:            
            if(Widget == WidgetInfo.Widget):
                self.WidgetListSelect.pop(Offset)
                return 1
            Offset += 1

        return 0
    #该函数主要给选中控件修改使用，只有当选中控件只有一个时才能使用
    def GetFirstSelectWidgetInfo(self): 
        if(1 != len(self.WidgetListSelect)):
            return -1,"NULL"
        
        return 0,self.WidgetListSelect[0]
    """ 删除指定控件 """
    def DelWidgetByWidget(self,Widget): 
        #如果删除的是框架，则需要删除所有使用该框架的控件
        if(self.DelFrameWidgetInfoByWidget(Widget)):
            for WidgetInfo in self.WidgetList:  
                if(WidgetInfo.Widget.master == Widget):
                    self.DelWidgetByWidget(WidgetInfo.Widget)
        self.DelWidgetInfoByWidget(Widget)
        self.DelSelectWidgetInfoByWidget(Widget)
        #删除控件
        Widget.destroy()
        self.labelSelectWidgetDel()
        
    def UpdataWidget(self,OldName,NewName): 
        for WidgetInfo in self.WidgetListSelect:            
            if(OldName == WidgetInfo.WidgetName):
                WidgetInfo.WidgetName = NewName
                break
                
        for WidgetInfo in self.FrameWidgetList:            
            if(OldName == WidgetInfo.WidgetName):
                WidgetInfo.WidgetName = NewName
                break
        for WidgetInfo in self.WidgetList:            
            if(OldName == WidgetInfo.WidgetName):
                WidgetInfo.WidgetName = NewName
                break    
    
    # 比较两个字符串前几个字符是否一样
    def StrCmpHead(self,Name1,Name2,CmpLen):
        if(len(Name1) < CmpLen):
            return False
        if(len(Name2) < CmpLen):
            return False
        TempName1 = Name1[0:CmpLen]
        TempName2 = Name2[0:CmpLen]
        if(TempName1==TempName2):     
            return True
        else:            
            return False
    def WidgetIsFrame(self,widget):
        WidgetTypeStr = '{}'.format(widget)
        #print("WidgetTypeStr=",WidgetTypeStr)
        WidgetSplit = WidgetTypeStr.split('!')
        #print("WidgetTypeStrEnd=",WidgetSplit[len(WidgetSplit)-1])
        
        #print(WidgetSplit,len(WidgetSplit))
        ret = self.StrCmpHead("labelframe",WidgetSplit[len(WidgetSplit)-1],len("labelframe"))
        if(ret):
            #print("Is Frame!!")
            return True
        else:
            #print("Not Frame!!")
            return False
            
    def WidgetIsSelect(self,widget):
        if(self.WidgetIsFrame(widget)):
            if(widget["fg"]=="red"):
                return True
            else:
                return False
        else:
            #if(widget["fg"]=="red"):
            if(widget["state"]==DISABLED):
                return True
            else:
                return False
    def WidgetSelectSet(self,widget,enable):
        if(self.WidgetIsFrame(widget)):
            if(enable == True):
                widget.config(fg = "red") 
            else:
                widget.config(fg = "black")
            
        else:
            if(enable == True):
                #widget.config(fg = "red")
                widget['state'] = DISABLED
            else:
                #widget.config(fg = "black")
                widget['state'] = NORMAL #NORMAL、ACTIVE、 DISABLED
    def WidgetStrTypeNameCmp(self,Widget,Name):
        WidgetTypeStr = '{}'.format(Widget)
        WidgetSplit = WidgetTypeStr.split('!')
        #print("WidgetTypeStrEnd=",WidgetSplit[len(WidgetSplit)-1])
        
        #print(WidgetSplit,len(WidgetSplit))
        return self.StrCmpHead(Name,WidgetSplit[len(WidgetSplit)-1],len(Name))
        
    def WidgetDump(self): 
        return  ##先屏蔽打印信息
        print("控件位置信息:")
        for widgetInfo in self.WidgetList:
            widget = widgetInfo.Widget
            print('组件:{}'.format(widget),' text:',widget['text']," place:",(widget.place_info())['x'],(widget.place_info())['y'])

            if(True !=self.WidgetStrTypeNameCmp(widget,"entry")):
                print("height:",widget['height'],"width",widget['width'])

            print("keys=",widget.keys())
            master = widget.master
            print("master=",master)
            print('上级组件:{}'.format(master))
    # 判断控件名是否已被占用
    def WidgetIsExist(self,Name): 
        Offset = 0
        for WidgetInfo in self.WidgetList:            
            if(Name == WidgetInfo.WidgetName):
                return 1            
        return 0
    def FindWidgetOffset(self,widget): 
        Offset = 0
        for WidgetInfo in self.WidgetList:
            i = WidgetInfo.Widget
            if(i == widget):
                return Offset
            Offset += 1
        return -1
    def DisableOneWidget(self,widget): 
        self.WidgetListSelect = []
        for WidgetInfo in self.WidgetList:
            i = WidgetInfo.Widget
            #print("------",i['text'])
            #print(i['state'],i['text'])
            if(i != widget):      
                self.WidgetSelectSet(i,False)
                #i.config(fg = "black")
                #i.config(bg = "black")
                #i['state'] = NORMAL #NORMAL、ACTIVE、 DISABLED
            else:
                #if(i["fg"]=="red"):
                if(self.WidgetIsSelect(i)):
                    self.WidgetSelectSet(i,False)
                    self.labelSelectWidgetDel()
                    #i.config(fg = "black")
                    #i.config(bg = "black")
                else:
                    self.WidgetSelectSet(i,True)
                    self.labelSelectWidgetShow(i)
                    self.WidgetListSelect.append(self.AutoDragWidgetInfo(WidgetInfo.WidgetName,WidgetInfo.Widget))
                    #i.config(fg = "red")
                    #i.config(bg = "red")
                #i['state'] = DISABLED #NORMAL、ACTIVE、 DISABLED
        return 
    #所有被选中的控件移动位置
    def WidgetPlaceSelect(self,widget,x,y):
        ret,SelectWidget = self.GetFirstSelectWidgetInfo()
        for WidgetInfo in self.WidgetList:
            i = WidgetInfo.Widget
            #print("------",i['text'])
            #print(i['state'],i['text'])
            if(i != widget):   
                #if(i["fg"]=="red"):
                if(self.WidgetIsSelect(i)):
                    i.place(x = int((i.place_info())['x'])+x, y=int((i.place_info())['y'])+y)
              
            else:
                i.place(x = int((i.place_info())['x'])+x, y=int((i.place_info())['y'])+y)
            if(ret == 0):
                if(SelectWidget.Widget == i):
                    self.labelSelectWidgetShow(i)
        return 
        
    #左键单击
    def Button1Callback(self,event): 
        #print("click offset",event.x,event.y)
        self.offset_x = event.x
        self.offset_y = event.y

        #widget = event.widget
        #Offset = self.FindWidgetOffset(widget)
        #print("Offset = ",Offset)
        #print('左键单击事件:{}'.format(self.WidgetList[Offset]),' text:',self.WidgetList[Offset]['text'])
    #左键拖拽
    def B1MotionCallback(self,event): 
        # 事件的属性
        widget = event.widget
        
        #widget.place(x = event.x+int((widget.place_info())['x'])-self.offset_x, y=event.y+int((widget.place_info())['y'])-self.offset_y)
        
        self.WidgetPlaceSelect(widget,event.x-self.offset_x,event.y-self.offset_y)
        #print("B1MotionCallback click at",event.x,event.y)    
        #print ("place info",(widget.place_info())['x'],(widget.place_info())['y'])
        #print("B1MotionCallback click at",event.x,event.y)
        #print("offset at",self.offset_x,self.offset_y)
        #print ("winfo_reqwidth info",widget.winfo_reqwidth())
        #print("place :",widget.winfo_x(),widget.winfo_y())#窗体位置
        
        #print('触发事件的组件:{}'.format(widget))
        #print ("place info",(widget.place_info())['x'],(widget.place_info())['y'])#控件位置
        
        #print('组件颜色:{}'.format(widget.cget('bg')))
        #widget_x = event.x  # 相对于组件的横坐标x
        #print('相对于组件的横坐标:{}'.format(widget_x))
        #widget_y = event.y  # 相对于组件的纵坐标y
        #print('相对于组件的纵坐标:{}'.format(widget_y))
        #x_root = event.x_root  # 相对于屏幕的左上角的横坐标
        #print('相对于屏幕的左上角的横坐标:{}'.format(x_root))
        #y_root = event.y_root  # 相对于屏幕的左上角的纵坐标
        #print('相对于屏幕的左上角的纵坐标:{}'.format(y_root))
    #左键释放
    def Button1ReleaseCallback(self,event): 
        #widget = event.widget
        #name='{}'.format(widget) 
        #print('触发事件的组件:{}'.format(widget),' text:',widget['text']," place:",(widget.place_info())['x'],(widget.place_info())['y'])
        
        self.WidgetDump()
        
    #右键单击
    def Button3Callback(self,event): 
        if(len(self.WidgetListSelect) > 0):
            return
        #print("click offset",event.x,event.y)
        self.offset3_x = event.x
        self.offset3_y = event.y      
    
    #右键双击击
    def DoubleButton3Callback(self,event): 
        widget = event.widget
        self.DisableOneWidget(widget)
    #右键拖拽
    def B3MotionCallback(self,event): 
        if(len(self.WidgetListSelect) > 0):
            return
        # 事件的属性
        widget = event.widget
        if(True !=self.WidgetStrTypeNameCmp(widget,"entry")):                                  
            height = int(widget['height']+(event.y-self.offset3_y))
            if(height >0):
                widget.config(height = height)
            self.offset3_y=event.y
        width = int(widget['width']+(event.x-self.offset3_x))
        if(width > 0):
            widget.config(width = width)
        self.offset3_x=event.x
        
        ret,SelectWidget = self.GetFirstSelectWidgetInfo()
        if(ret == 0):
            if(SelectWidget.Widget == widget):
                self.labelSelectWidgetShow(widget)
        #print("height=",widget['height'],"offset=",event.x-self.offset3_x)
        #widget.place(x = event.x+int((widget.place_info())['x'])-self.offset_x, y=event.y+int((widget.place_info())['y'])-self.offset_y)
        
    #右键释放
    def Button3ReleaseCallback(self,event): 
        if(len(self.WidgetListSelect) > 0):
            return
        #widget = event.widget
        #name='{}'.format(widget) 
        #print('触发事件的组件:{}'.format(widget),' text:',widget['text']," place:",(widget.place_info())['x'],(widget.place_info())['y'])
        
        self.WidgetDump()
    #shift 加右键
    def Button3ShiftCallback(self,event): 
        widget = event.widget
        #widget['state'] = DISABLED #NORMAL、ACTIVE、 DISABLED
        #widget.config(fg = "red")
        if(True != self.WidgetIsSelect(widget)):
            self.labelSelectWidgetDel()
            self.WidgetSelectSet(widget,True)
            ret,WidgetInfo = self.FindWidgetInfoByWidget(widget)
            if(0 == ret):
                self.WidgetListSelect.append(self.AutoDragWidgetInfo(WidgetInfo.WidgetName,WidgetInfo.Widget))
        #widget = event.widget
        #name='{}'.format(widget) 
        #print('触发事件的组件:{}'.format(widget),' text:',widget['text']," place:",(widget.place_info())['x'],(widget.place_info())['y'])
        
        self.WidgetDump()
        
    #绑定控件
    def DragBind(self,name,widget): 
        
        #左键拖拽
        widget.bind("<B1-Motion>",self.B1MotionCallback)
        #放开左键
        widget.bind("<ButtonRelease-1>",self.Button1ReleaseCallback,add="+")  
        #单击左键
        widget.bind("<Button-1>",self.Button1Callback,add="+")  
       
        #右键拖拽
        widget.bind("<B3-Motion>",self.B3MotionCallback,add="+")
        #放开右键
        widget.bind("<ButtonRelease-3>",self.Button3ReleaseCallback,add="+")  
        #单击右键
        widget.bind("<Button-3>",self.Button3Callback,add="+")  
        #shift右键
        widget.bind("<Shift-Button-3>", self.Button3ShiftCallback,add="+")
        #双击右键
        widget.bind("<Double-Button-3>", self.DoubleButton3Callback,add="+")
        
        self.WidgetList.append(self.AutoDragWidgetInfo(name,widget))
        
        
    """
    控制按钮实现
    """
    """
    当前选中控件实现
    """
    def labelSelectWidgetShow(self,Widget):
        
        ret,FatherWidgetInfo = self.FindFrameWidgetInfoByWidget(Widget.master)
        if(ret == 0):
            #print("father name=",FatherWidgetInfo.WidgetName)
            #print("FrameWidgetValueList len=",len(self.FrameWidgetValueList))
            iCount = 0
            for WidgetValue in self.FrameWidgetValueList:    
                #print("father name=",FatherWidgetInfo.WidgetName)
                #print("WidgetValuee=",WidgetValue)
                if(WidgetValue == FatherWidgetInfo.WidgetName):
                    self.labelSelectWidgetMasterText.current(iCount)
                    #print("current=",iCount)
                    break
                iCount += 1

                        
        self.labelSelectWidgetNameText.delete(0,END)   
        self.labelSelectWidgetDesText.delete(0,END)     
        self.labelSelectWidgetPlaceXText.delete(0,END) 
        self.labelSelectWidgetPlaceYText.delete(0,END) 
        self.labelSelectWidgetAreaXText.delete(0,END) 
        self.labelSelectWidgetAreaYText.delete(0,END) 
        
        ret,WidgetInfo = self.FindWidgetInfoByWidget(Widget)
        if(ret == 0):        
            self.labelSelectWidgetNameText.insert(0,WidgetInfo.WidgetName)              

        self.labelSelectWidgetDesText.insert(0,Widget['text'])                

        self.labelSelectWidgetPlaceXText.insert(0,(Widget.place_info())['x'])
        self.labelSelectWidgetPlaceYText.insert(0,(Widget.place_info())['y'])        
       
        if(True !=self.WidgetStrTypeNameCmp(Widget,"entry")):                      
            self.labelSelectWidgetAreaYText.insert(0,Widget['height'])
        else:
            self.labelSelectWidgetAreaXText.insert(0,"")
            
        self.labelSelectWidgetAreaXText.insert(0,Widget['width'])
    def labelSelectWidgetDel(self):
        self.labelSelectWidgetMasterText.current(0)
        
        self.labelSelectWidgetNameText.delete(0,END)   
        self.labelSelectWidgetDesText.delete(0,END)     
        self.labelSelectWidgetPlaceXText.delete(0,END) 
        self.labelSelectWidgetPlaceYText.delete(0,END) 
        self.labelSelectWidgetAreaXText.delete(0,END) 
        self.labelSelectWidgetAreaYText.delete(0,END) 
    def ButtonApplySelectWidget(self):
        WidgetName = self.labelSelectWidgetNameText.get()
        WidgetFatherName = self.labelSelectWidgetMasterText.get()
        WidgetDes = self.labelSelectWidgetDesText.get()
        WidgetPlaceX = self.labelSelectWidgetPlaceXText.get()     
        WidgetPlaceY = self.labelSelectWidgetPlaceYText.get()        
        WidgetAreaX = self.labelSelectWidgetAreaXText.get()     
        WidgetAreaY = self.labelSelectWidgetAreaYText.get()
             
        
        #print("WidgetName=",WidgetName)
        #print("WidgetFatherName=",WidgetFatherName)
        #print("WidgetDes=",WidgetDes)
        #print("WidgetPlaceX=",WidgetPlaceX)
        #print("WidgetPlaceY=",WidgetPlaceY)
        #print("WidgetAreaX=",WidgetAreaX)
        #print("WidgetAreaY=",WidgetAreaY)
                
        ret,FatherWidget = self.FindFrameWidget(WidgetFatherName)
        if(ret != 0):
            print("FatherWidget ",WidgetFatherName,"is not exist!! please change it!!")
            return -1
        
        ret,WidgetInfoSelect = self.GetFirstSelectWidgetInfo()
        if(ret != 0):
            print("No widget is select !!")
            return -1
        if(WidgetName != WidgetInfoSelect.WidgetName):  
            if(1 == self.WidgetIsExist(WidgetName)):
                print("WidgetName ",WidgetName,"is exist!! please change it!!")
                return -1
            self.UpdataWidget(WidgetInfoSelect.WidgetName,WidgetName)
        WidgetInfoSelect.Widget['text'] =  WidgetDes
        #WidgetInfoSelect.Widget.master = FatherWidget        
        
        WidgetInfoSelect.Widget.place(x = int(WidgetPlaceX), y=int(WidgetPlaceY))        
        WidgetInfoSelect.Widget.config(width = int(WidgetAreaX))
        if(True !=self.WidgetStrTypeNameCmp(WidgetInfoSelect.Widget,"entry")):                                  
            WidgetInfoSelect.Widget.config(height = int(WidgetAreaY))
        
    def ButtonDelSelectWidget(self):
        ret,WidgetInfoSelect = self.GetFirstSelectWidgetInfo()
        if(ret != 0):
            print("No widget is select !!")
            return -1
        self.DelWidgetByWidget(WidgetInfoSelect.Widget)
        
    """
    微调实现
    """
    def ButtonUpSelectWidget(self):
        WidgetPlaceStep = self.labelSelectWidgetPlaceSteText.get()  
        self.WidgetPlaceSelect("",0,-int(WidgetPlaceStep))
        
    def ButtonDownSelectWidget(self):
        WidgetPlaceStep = self.labelSelectWidgetPlaceSteText.get()  
        self.WidgetPlaceSelect("",0,int(WidgetPlaceStep))
        
    def ButtonLeftSelectWidget(self):
        WidgetPlaceStep = self.labelSelectWidgetPlaceSteText.get()  
        self.WidgetPlaceSelect("",-int(WidgetPlaceStep),0)
        
    def ButtonRightSelectWidget(self):
        WidgetPlaceStep = self.labelSelectWidgetPlaceSteText.get()  
        self.WidgetPlaceSelect("",int(WidgetPlaceStep),0)
    """
    控件对齐实现
    """
    
    
    def ButtonAlignRightSelectWidget(self):
        if(len(self.WidgetListSelect) <= 1):
            return
        i = self.WidgetListSelect[0].Widget
        BaseX = int((i.place_info())['x'])
        
        for WidgetInfo in self.WidgetListSelect:            
            i = WidgetInfo.Widget
            #i.place(x = BaseX, y=int((i.place_info())['y']))
            i.place(x = BaseX)

        return 0
    def ButtonAlignLeftSelectWidget(self):
        
        return 0
    def ButtonAlignTopSelectWidget(self):
        if(len(self.WidgetListSelect) <= 1):
            return
        i = self.WidgetListSelect[0].Widget
        BaseY = int((i.place_info())['y'])
        
        for WidgetInfo in self.WidgetListSelect:            
            i = WidgetInfo.Widget
            #i.place(x = int((i.place_info())['x']), y=BaseY)
            i.place(y=BaseY)

        return 0
    def ButtonAlignButtonSelectWidget(self):
        
        return 0
    
    def ButtonAlignXAvgSelectWidget(self):
        
        if(len(self.WidgetListSelect) <= 1):
            return
        i = self.WidgetListSelect[0].Widget
        StartX = int((i.place_info())['x'])
        
        i = self.WidgetListSelect[len(self.WidgetListSelect)-1].Widget
        EndX = int((i.place_info())['x'])
        
        Step = len(self.WidgetListSelect) - 1
        AvgStep = int((EndX - StartX)/Step)
        
        iCount = 0
        for WidgetInfo in self.WidgetListSelect:            
            i = WidgetInfo.Widget
            #i.place(x = int((i.place_info())['x']), y=BaseY)
            i.place(x=int(StartX+iCount * AvgStep))
            iCount += 1

        return 0
    def ButtonAlignYAvgSelectWidget(self):
        
        if(len(self.WidgetListSelect) <= 1):
            return
        i = self.WidgetListSelect[0].Widget
        StartY = int((i.place_info())['y'])
        
        i = self.WidgetListSelect[len(self.WidgetListSelect)-1].Widget
        EndY = int((i.place_info())['y'])
        
        Step = len(self.WidgetListSelect) - 1
        AvgStep = int((EndY - StartY)/Step)
        
        iCount = 0
        for WidgetInfo in self.WidgetListSelect:            
            i = WidgetInfo.Widget
            #i.place(x = int((i.place_info())['x']), y=BaseY)
            i.place(y=int(StartY+iCount * AvgStep))
            iCount += 1

        return 0

    """
    新增控件
    """
    def ButtonNewWidgetInfoGet(self):
        WidgetName = self.labelNewWidgetNameText.get()
        WidgetFatherName = self.labelNewWidgetMasterText.get()
        
        #print("WidgetName=",WidgetName)
        #print("WidgetFatherName=",WidgetFatherName)
        
        if(1 == self.WidgetIsExist(WidgetName)):
            print("WidgetName ",WidgetName,"is exist!! please change it!!")
            return -1, "", ""
        ret,FatherWidget = self.FindFrameWidget(WidgetFatherName)
        if(ret != 0):
            print("FatherWidget ",WidgetFatherName,"is not exist!! please change it!!")
            return -1, "", ""
        return 0, WidgetName,FatherWidget
    
    def ButtonNewWidgetButton(self):
        ret,WidgetName,FatherWidget=self.ButtonNewWidgetInfoGet()
        if(ret != 0):
            print("ButtonNewWidgetInfoGet Error!! please check it!!")
            return 0
         
        Widget=Button(FatherWidget,text=WidgetName,width=10)
        Widget.place(x=5,y=5)
        
        #加入控件列表
        self.DragBind(WidgetName,Widget)
        
        #强制选中新增控件
        self.DisableOneWidget(Widget)
        return
    def ButtonNewWidgetLable(self):
        ret,WidgetName,FatherWidget=self.ButtonNewWidgetInfoGet()
        if(ret != 0):
            print("ButtonNewWidgetInfoGet Error!! please check it!!")
            return 0
          
        Widget=Label(FatherWidget,text=WidgetName,height=1,width=10)
        Widget.place(x=5,y=5)
        
        #加入控件列表
        self.DragBind(WidgetName,Widget)
        
        #强制选中新增控件
        self.DisableOneWidget(Widget)
        return
    def ButtonNewWidgetEntry(self):
        ret,WidgetName,FatherWidget=self.ButtonNewWidgetInfoGet()
        if(ret != 0):
            print("ButtonNewWidgetInfoGet Error!! please check it!!")
            return 0

        Widget=Entry(FatherWidget,text=WidgetName,width=10,relief="groove")
        Widget.place(x=5,y=5)
        
        #加入控件列表
        self.DragBind(WidgetName,Widget)
        
        #强制选中新增控件
        self.DisableOneWidget(Widget)
        return
    def ButtonNewWidgetCombobox(self):
        ret,WidgetName,FatherWidget=self.ButtonNewWidgetInfoGet()
        if(ret != 0):
            print("ButtonNewWidgetInfoGet Error!! please check it!!")
            return 0
 
        Widget=ttk.Combobox(FatherWidget,text=WidgetName,width=10)
        Widget.place(x=5,y=5)
        
        #加入控件列表
        self.DragBind(WidgetName,Widget)
        
        #强制选中新增控件
        self.DisableOneWidget(Widget)
        return
    def ButtonNewWidgetLableFrame(self):
        ret,WidgetName,FatherWidget=self.ButtonNewWidgetInfoGet()
        if(ret != 0):
            print("ButtonNewWidgetInfoGet Error!! please check it!!")
            return 0
        
        Widget=LabelFrame(FatherWidget,text=WidgetName,height=100,width=100)
        Widget.place(x=5,y=5)
        
        # 加入框架控件列表
        self.FrameWidgetList.append(self.AutoDragWidgetInfo(WidgetName,Widget))
        self.FrameWidgetNameUpdate()
        
        #加入控件列表
        self.DragBind(WidgetName,Widget)
        
        #强制选中新增控件
        self.DisableOneWidget(Widget)
        
        return
    """输出显示"""
    def WidgetConfigShow(self,Widget):
        
        ret,FatherWidgetInfo = self.FindFrameWidgetInfoByWidget(Widget.master)
        if(ret != 0):
            print("WidgetConfigShow get father error!!")
            return -1
        
        #print("father name=",FatherWidgetInfo.WidgetName)
        #print("FrameWidgetValueList len=",len(self.FrameWidgetValueList))
            
        
        ret,WidgetInfo = self.FindWidgetInfoByWidget(Widget)
        if(ret != 0):    
            print("WidgetConfigShow FindWidgetInfoByWidget error!!")
            return -1 
        WidgetCreateStr = ""
        """显示控件命令"""
        #WidgetTypeStr = '{}'.format(Widget)
        #print("WidgetTypeStr=",WidgetTypeStr)
        if(self.WidgetStrTypeNameCmp(Widget,"entry")):             
            WidgetCreateStr = "self."+WidgetInfo.WidgetName+"=Entry("+ "self."+FatherWidgetInfo.WidgetName+",text=\""
            WidgetCreateStr +=Widget['text']
            WidgetCreateStr +="\",width="+str(Widget['width'])+",relief=\"groove\")"
        elif(self.WidgetStrTypeNameCmp(Widget,"labelframe")):      
            
            WidgetCreateStr = "self."+WidgetInfo.WidgetName+"=LabelFrame("+ "self."+FatherWidgetInfo.WidgetName+",text=\""
            WidgetCreateStr +=Widget['text']
            WidgetCreateStr +="\",width="+str(Widget['width'])
            WidgetCreateStr +=",height="+str(Widget['height'])+")"
        elif(self.WidgetStrTypeNameCmp(Widget,"label")):            
            WidgetCreateStr = "self."+WidgetInfo.WidgetName+"=Label("+ "self."+FatherWidgetInfo.WidgetName+",text=\""
            WidgetCreateStr +=Widget['text']
            WidgetCreateStr +="\",width="+str(Widget['width'])
            WidgetCreateStr +=",height="+str(Widget['height'])+")"
            
        elif(self.WidgetStrTypeNameCmp(Widget,"combobox")): 
            WidgetCreateStr = "self."+WidgetInfo.WidgetName+"=ttk.Combobox("+ "self."+FatherWidgetInfo.WidgetName+",text=\""
            WidgetCreateStr +=Widget['text']
            WidgetCreateStr +="\",width="+str(Widget['width'])
            WidgetCreateStr +=",height="+str(Widget['height'])+")"
        elif(self.WidgetStrTypeNameCmp(Widget,"button")):            
            WidgetCreateStr = "self."+WidgetInfo.WidgetName+"=Button("+ "self."+FatherWidgetInfo.WidgetName+",text=\""
            WidgetCreateStr +=Widget['text']
            WidgetCreateStr +="\",width="+str(Widget['width'])
            WidgetCreateStr +=",height="+str(Widget['height'])+")"       
        
            
        print(WidgetCreateStr)
        WidgetPlaceStr = "self."+WidgetInfo.WidgetName+".place(x="
        WidgetPlaceStr += str((Widget.place_info())['x'])+",y="
        WidgetPlaceStr +=str((Widget.place_info())['y'])+")"
        print(WidgetPlaceStr)
    def ButtonWidgetDump(self):
        print("=====================================")
        print("控件分配：")
        for widgetInfo in self.WidgetList:
            widget = widgetInfo.Widget
            self.WidgetConfigShow(widget)
        
    #框架节点数据更新
    def FrameWidgetNameUpdate(self):
        self.FrameWidgetValueList = []
        for WidgetInfo in self.FrameWidgetList:
            self.FrameWidgetValueList.append(WidgetInfo.WidgetName)
        self.labelSelectWidgetMasterText["value"] = self.FrameWidgetValueList
        self.labelNewWidgetMasterText["value"] = self.FrameWidgetValueList
        
        self.labelSelectWidgetMasterText.current(0)
        self.labelNewWidgetMasterText.current(0)
    def ViewDrawFrame(self):
        # 创建labFrame
        self.labFrameSelectWidget=LabelFrame(self.root,text="选中控件信息",height=195,width=255)
        self.labelSelectWidgetMaster = Label(self.labFrameSelectWidget,text="父节点:",width = 10)
        self.labelSelectWidgetMasterText = ttk.Combobox(self.labFrameSelectWidget,text="SelectWidgetMasterText", width = 20)
        #self.labelSelectWidgetMasterText.insert(0,"SelectWidgetMasterText")
        self.labelSelectWidgetName = Label(self.labFrameSelectWidget,text="节点名称:",width = 10)
        self.labelSelectWidgetNameText = Entry(self.labFrameSelectWidget,text="labelSelectWidgetNameText", width = 20,relief="groove")
        self.labelSelectWidgetNameText.insert(0,"labelSelectWidgetNameText")
        
        self.labelSelectWidgetDes = Label(self.labFrameSelectWidget,text="描述:",width = 10)
        self.labelSelectWidgetDesText = Entry(self.labFrameSelectWidget,text="SelectWidgetDesText", width = 20,relief="groove")
        self.labelSelectWidgetDesText.insert(0,"SelectWidgetDesText")
                
        self.labelSelectWidgetPlace = Label(self.labFrameSelectWidget,text="位置:",width = 10)
        self.labelSelectWidgetPlaceXText = Entry(self.labFrameSelectWidget,text="PlaceX", width = 5,relief="groove")
        self.labelSelectWidgetPlaceXText.insert(0,"PlaceX")
        self.labelSelectWidgetPlaceYText = Entry(self.labFrameSelectWidget,text="PlaceY", width = 5,relief="groove")
        self.labelSelectWidgetPlaceYText.insert(0,"PlaceY")
        
        self.labelSelectWidgetArea = Label(self.labFrameSelectWidget,text="大小:",width = 10)
        self.labelSelectWidgetAreaXText = Entry(self.labFrameSelectWidget,text="AreaX", width = 5,relief="groove")
        self.labelSelectWidgetAreaXText.insert(0,"AreaX")
        self.labelSelectWidgetAreaYText = Entry(self.labFrameSelectWidget,text="AreaY", width = 5,relief="groove")
        self.labelSelectWidgetAreaYText.insert(0,"AreaY")
        
        self.btnSelectWidgetApply = Button(self.labFrameSelectWidget,text="应用",width = 10,height=1,
                                          command = self.ButtonApplySelectWidget)
        self.btnSelectWidgetDel = Button(self.labFrameSelectWidget,text="删除",width = 10,height=1,
                                         command = self.ButtonDelSelectWidget)
        
        # 创建微调控件
        self.labFramePlaceStep=LabelFrame(self.root,text="微调",height=148,width=255)    
                                                            
        self.labelSelectWidgetPlaceStep = Label(self.labFramePlaceStep,text="微调粒度:",width = 10)
        self.labelSelectWidgetPlaceSteText = Entry(self.labFramePlaceStep,text="SelectWidgetPlaceStep", width = 20,relief="groove")
        self.labelSelectWidgetPlaceSteText.insert(0,"1")
        
        self.btnSelectWidgetPlaceUp = Button(self.labFramePlaceStep,text="上移",width = 5,height=1,
                                            command = self.ButtonUpSelectWidget)
        self.btnSelectWidgetPlaceDown = Button(self.labFramePlaceStep,text="下移",width = 5,height=1,
                                              command = self.ButtonDownSelectWidget)
        self.btnSelectWidgetPlaceLeft = Button(self.labFramePlaceStep,text="左移",width = 5,height=1,
                                              command = self.ButtonLeftSelectWidget)
        self.btnSelectWidgetPlaceRight = Button(self.labFramePlaceStep,text="右移",width = 5,height=1,
                                               command = self.ButtonRightSelectWidget)
        
        # 多路控件调整
        self.labFrameAlign=LabelFrame(self.root,text="控件对齐",height=119,width=255)
        
        self.btnAlignRight = Button(self.labFrameAlign,text="左对齐",width = 8,height=1,
                                   command = self.ButtonAlignRightSelectWidget)
        self.btnAlignLeft = Button(self.labFrameAlign,text="右对齐",width = 8,height=1,
                                  command = self.ButtonAlignLeftSelectWidget)
        self.btnAlignTop = Button(self.labFrameAlign,text="顶部对齐",width = 8,height=1,
                                 command = self.ButtonAlignTopSelectWidget)
        self.btnAlignButton = Button(self.labFrameAlign,text="底部对齐",width = 8,height=1,
                                    command = self.ButtonAlignButtonSelectWidget) 
        self.btnAlignXAvg = Button(self.labFrameAlign,text="均匀分布(X)",width = 10,height=1,
                                  command = self.ButtonAlignXAvgSelectWidget)
        self.btnAlignYAvg = Button(self.labFrameAlign,text="均匀分布(Y)",width = 10,height=1,
                                  command = self.ButtonAlignYAvgSelectWidget)
        
         # 控件类型
        self.labFrameNewWidget=LabelFrame(self.root,text="控件",height=190,width=255)
        self.labelNewWidgetMaster = Label(self.labFrameNewWidget,text="控件父节点:",width = 10)
        self.labelNewWidgetMasterText = ttk.Combobox(self.labFrameNewWidget,text="NewtWidgetMasterText", width = 20)
        #self.labelNewWidgetMasterText.insert(0,"NewWidgetMasterText")        
            
        self.labelNewWidgetName = Label(self.labFrameNewWidget,text="节点名称:",width = 10)
        self.labelNewWidgetNameText = Entry(self.labFrameNewWidget,text="labelNewWidgetNameText", width = 20,relief="groove")
        self.labelNewWidgetNameText.insert(0,"WidgetName")
        
        self.btnNewWidgetButton = Button(self.labFrameNewWidget,text="Button",width = 10,height=1,
                                         command = self.ButtonNewWidgetButton)
        self.btnNewWidgetLable = Button(self.labFrameNewWidget,text="Lable",width = 10,height=1,
                                        command = self.ButtonNewWidgetLable)
        self.btnNewWidgetEntry = Button(self.labFrameNewWidget,text="Entry",width = 10,height=1,
                                        command = self.ButtonNewWidgetEntry)
        self.btnNewWidgetCombobox = Button(self.labFrameNewWidget,text="Combobox",width = 10,height=1,
                                           command = self.ButtonNewWidgetCombobox)
        self.btnNewWidgetLableFrame = Button(self.labFrameNewWidget,text="LableFrame",width = 20,height=1,
                                             command = self.ButtonNewWidgetLableFrame)
        
        self.btnDump = Button(self.root,text="输出数据",font = self.ft,width = 20,height=2,
                                  command = self.ButtonWidgetDump)
        
        #图形界面分布
        
        # 创建labFrame
        self.labFrameSelectWidget.place(x=5,y=5)
        self.labelSelectWidgetMaster.place(x=5,y=5)
        self.labelSelectWidgetMasterText.place(x=86,y=5)        
        
        self.labelSelectWidgetName.place(x=5,y=34)
        self.labelSelectWidgetNameText.place(x=86,y=34)
        
        self.labelSelectWidgetDes.place(x=5,y=58)
        self.labelSelectWidgetDesText.place(x=86,y=58)
                        
        self.labelSelectWidgetPlace.place(x=5,y=83)
        self.labelSelectWidgetPlaceXText.place(x=86,y=83)        
        self.labelSelectWidgetPlaceYText.place(x=140,y=83)
        
        
        self.labelSelectWidgetArea.place(x=5,y=111)
        self.labelSelectWidgetAreaXText.place(x=86,y=111)        
        self.labelSelectWidgetAreaYText.place(x=140,y=111)
        
        
        self.btnSelectWidgetApply.place(x=27,y=141)
        self.btnSelectWidgetDel.place(x=144,y=141)
        
        self.labFramePlaceStep.place(x=5,y=205)
        self.labelSelectWidgetPlaceStep.place(x=5,y=10)
        self.labelSelectWidgetPlaceSteText.place(x=75,y=10)          
        self.btnSelectWidgetPlaceUp.place(x=85,y=50)
        self.btnSelectWidgetPlaceDown.place(x=85,y=85)
        self.btnSelectWidgetPlaceLeft.place(x=30,y=85)
        self.btnSelectWidgetPlaceRight.place(x=140,y=85)
        
        # 多路控件调整
        self.labFrameAlign.place(x=5,y=366)
        
        self.btnAlignRight.place(x=5,y=10)
        self.btnAlignLeft.place(x=5,y=55)
        self.btnAlignTop.place(x=80,y=10)
        self.btnAlignButton.place(x=80,y=55)
        self.btnAlignXAvg.place(x=160,y=10)
        self.btnAlignYAvg.place(x=160,y=55)
        
         # 控件类型
        self.labFrameNewWidget.place(x=5,y=500)    
        
        self.labelNewWidgetMaster.place(x=5,y=5)
        self.labelNewWidgetMasterText.place(x=85,y=5)
        
        self.labelNewWidgetName.place(x=5,y=32)
        self.labelNewWidgetNameText.place(x=85,y=32)
                
        self.btnNewWidgetButton.place(x=23,y=64)
        self.btnNewWidgetLable.place(x=23,y=101)
        self.btnNewWidgetEntry.place(x=138,y=64)
        self.btnNewWidgetCombobox.place(x=138,y=101)
        
        self.btnNewWidgetLableFrame.place(x=50,y=134)
        
        
        self.btnDump.place(x=52,y=700)    
        
       
        #拖动绑定 
        """
        self.DragBind("labFrameSelectWidget",self.labFrameSelectWidget)
        self.DragBind("labelSelectWidgetMaster",self.labelSelectWidgetMaster)
        self.DragBind("labelSelectWidgetMasterText",self.labelSelectWidgetMasterText)
        self.DragBind("labelSelectWidgetName",self.labelSelectWidgetName)
        self.DragBind("labelSelectWidgetNameText",self.labelSelectWidgetNameText)
        self.DragBind("labelSelectWidgetDes",self.labelSelectWidgetDes)
        self.DragBind("labelSelectWidgetDesText",self.labelSelectWidgetDesText)
        self.DragBind("labelSelectWidgetPlace",self.labelSelectWidgetPlace)
        self.DragBind("labelSelectWidgetPlaceXText",self.labelSelectWidgetPlaceXText)
        self.DragBind("labelSelectWidgetPlaceYText",self.labelSelectWidgetPlaceYText)   
        self.DragBind("labelSelectWidgetArea",self.labelSelectWidgetArea)
        self.DragBind("labelSelectWidgetAreaXText",self.labelSelectWidgetAreaXText)
        self.DragBind("labelSelectWidgetAreaYText",self.labelSelectWidgetAreaYText)
        self.DragBind("btnSelectWidgetApply",self.btnSelectWidgetApply)
        self.DragBind("btnSelectWidgetDel",self.btnSelectWidgetDel)
        

        self.DragBind("labFramePlaceStep",self.labFramePlaceStep)        
        self.DragBind("labelSelectWidgetPlaceStep",self.labelSelectWidgetPlaceStep)
        self.DragBind("labelSelectWidgetPlaceSteText",self.labelSelectWidgetPlaceSteText)
        self.DragBind("btnSelectWidgetPlaceUp",self.btnSelectWidgetPlaceUp)
        self.DragBind("btnSelectWidgetPlaceDown",self.btnSelectWidgetPlaceDown)
        self.DragBind("btnSelectWidgetPlaceLeft",self.btnSelectWidgetPlaceLeft)
        self.DragBind("btnSelectWidgetPlaceRight",self.btnSelectWidgetPlaceRight)
        self.DragBind("labFrameAlign",self.labFrameAlign)
        self.DragBind("btnAlignRight",self.btnAlignRight)
        self.DragBind("btnAlignLeft",self.btnAlignLeft)
        self.DragBind("btnAlignTop",self.btnAlignTop)
        self.DragBind("btnAlignButton",self.btnAlignButton)
        self.DragBind("btnAlignXAvg",self.btnAlignXAvg)
        self.DragBind("btnAlignYAvg",self.btnAlignYAvg)
  
        self.DragBind("labFrameNewWidget",self.labFrameNewWidget)
        self.DragBind("labelNewWidgetMaster",self.labelNewWidgetMaster)
        self.DragBind("labelNewWidgetMasterText",self.labelNewWidgetMasterText)
        
        self.DragBind("labelNewWidgetName",self.labelNewWidgetName)
        self.DragBind("labelNewWidgetNameText",self.labelNewWidgetNameText)
          
        self.DragBind("btnNewWidgetButton",self.btnNewWidgetButton)
        self.DragBind("btnNewWidgetLable",self.btnNewWidgetLable)
        self.DragBind("btnNewWidgetEntry",self.btnNewWidgetEntry)
        self.DragBind("btnNewWidgetCombobox",self.btnNewWidgetCombobox)
        self.DragBind("btnNewWidgetLableFrame",self.btnNewWidgetLableFrame)   
 
        self.DragBind("btnDump",self.btnDump)   
        """

    def ViewDrawMain(self):
        self.root = Tk()
        self.root.title(self.viewName)

        self.ft = tf.Font( size=15)  #设置字体
        self.ftBig = tf.Font( size=25)  #设置字体
        #设置窗口大小
        self.root.geometry("260x750+1000+200")
        
        
        #图标
        #self.root.iconbitmap('temprature_view.ico')
        #控制窗口
        self.ViewDrawFrame()
        
        
        #显示窗体支持的属性
        #print(self.root.keys())
        #print(self.labelServerStatus.keys())
        #print("slave",self.root.slave)
        #print("use",self.root['use'])
        #print(self.labFrameCheck.keys())
    def ViewDrawThread(self):
        print("thread start")
        self.ViewDrawMain()
        self.FrameWidgetNameUpdate()
        #进入循环
        self.root.mainloop()
    def __init__(self,rootName,root):
        #初始化
        self.viewName = "页面布局"
           
        self.FrameWidgetList = []        
        self.FrameWidgetList.append(self.AutoDragWidgetInfo(rootName,root))
        
        print("root name = ",self.FrameWidgetList[0].WidgetName)
        print("root Widget.keys = ",self.FrameWidgetList[0].Widget.keys())
        self.WidgetList = []
        self.WidgetListSelect = []
        #创建线程
        print("create thread")
        thread = threading.Thread(target = self.ViewDrawThread)
        thread.start()
        return